package cn.ixinjiu.libraryMangement.controller;

import cn.ixinjiu.libraryMangement.entity.UserInfo;
import cn.ixinjiu.libraryMangement.service.IUserInfoService;
import cn.ixinjiu.libraryMangement.service.impl.UserInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/*
 * 注册用户处理
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private IUserInfoService userInfoService = new UserInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // 设置编码格式
            req.setCharacterEncoding("utf-8");
            resp.setHeader("Content-Type", "text/html;charset=utf-8");
            // 获取用户注册信息
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            UserInfo user = new UserInfo(null, username, password, null, email, phone, 0);
            System.out.println("user = " + user);
            int register = userInfoService.register(user);
            if (register == 1) {
                resp.sendRedirect("index.jsp");
            } else {
                System.out.println("fail");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
